import { ajax, post } from '@/utils/ajax';
import { ElMessage as Message } from 'element-plus';

// 获取静态流程图
export function showFlowStaticImg(wfCode) {
  const config = {
    url: '/wf/manager/deployment/showFlowStaticImg',
    method: 'post',
    data: { wfCode },
    responseType: 'arraybuffer'
  };
  return ajax(config);
}

// 获取流程过程流程图
export function showFlowRunImgByInstantId(processInstanceId) {
  const config = {
    url: '/wf/manager/deployment/showFlowRunImgByInstantId',
    method: 'post',
    data: { processInstanceId },
    responseType: 'arraybuffer'
  };
  return ajax(config);
}

// 发起流程并完成第一步
// export function startWfInstanceAndCompleteFirst(params) {
//   return post('/code/wf/model/startWfInstanceAndCompleteFirst', params);
// }

// 发起流程
export function startWfInstance(params) {
  return post('/wf/manager/instance/saveAndStartWfInstance', params);
}

// 未选择模型发起流程
export function startWfInstanceWithoutModel(params) {
  return post('/wf/manager/instance/startWfInstance', params);
}

/**
 * 启动工作流并选择下一步处理
 * @param wfCode 流程编码
 * @param businessKey 业务主键
 * @param modelName 模型名称
 * @param businessSubject 业务主题
 * @param completeFirst 是否完成第一步
 * @param formData 表单数据对象
 * @param params 其他流程参数
 * @param callback 完成后回调函数
 */
export function startWfInstanceAndSelectUser(compoment, wfCode, businessKey, modelName, businessSubject, completeFirst, formData, params, callback) {
  wfCode = wfCode || '';
  businessKey = businessKey || '';
  modelName = modelName || '';
  businessSubject = businessSubject || '';
  completeFirst = completeFirst || false;
  formData = formData || {};
  let isApprove = "true";
  params = params || {};
  callback = callback || function () {};
  // 删除启动人参数，后台从Session获取
  params && delete params.starter;
  isNeedSelectUser(
    wfCode,
    null,
    completeFirst,
    isApprove,
    formData,
    params,
    function () {
      //选择下一步处理
      var dest = {
        wfCode,
        businessKey,
        modelName,
        businessSubject,
        completeFirst,
        isApprove,
        formData,
        params,
        callback
      };
      if (compoment && typeof compoment.open == 'function') {
        compoment.open(dest);
      }
    },
    function () {
      const newParams = {
        wfCode,
        businessKey,
        modelName,
        businessSubject,
        candidates: '',
        completeFirst,
        isApprove: "true",
        params,
        ...formData
      };
      //判断有无模型
      if (modelName) {
        startWfInstance(newParams).then((res) => {
          if (callback && typeof callback == 'function') {
            callback(res);
          }
        });
      } else {
        startWfInstanceWithoutModel(newParams).then((res) => {
          if (callback && typeof callback == 'function') {
            callback(res);
          }
        });
      }
    }
  );
}

/**
 * 是否需要选择下一步处理人
 *
 * @param {String} wfCode 流程编码
 * @param {String} taskId 任务ID
 * @param {Boolean} completeFirst 是否完成第一步
 * @param {String} isApprove 是否同意，'true'表示同意
 * @param {String} formData 表单数据对象
 * @param {Object} params 参数对象
 * @param {Function} needCallback 需要选人回调
 * @param {Function} notNeedCallback 不需要选人回调
 */
export function isNeedSelectUser(wfCode, taskId, completeFirst, isApprove, formData, params, needCallback, notNeedCallback) {
  wfCode = wfCode || '';
  taskId = taskId || '';
  isApprove = isApprove || '';
  formData = formData || {};
  params = params || {};
  completeFirst = completeFirst || false;
  if (wfCode === '' && taskId === '') {
    Message({
      message: '流程编码和任务ID不可同时为空',
      type: 'error'
    });
    return;
  }
  return post('/wf/manager/instance/isNeedSelectUser', { wfCode, taskId, completeFirst, isApprove, params, ...formData }, { objectAsMap: true }).then((res) => {
    if (res.code === '200' || res.code === 200) {
      if (res.data === 'true') {
        needCallback();
      } else {
        notNeedCallback();
      }
    } else {
      Message({
        message: res.msg,
        type: 'error'
      });
    }
  });
}
